class DataUtils {

    private static rawData;

    public static async load() {
        if (egret.Capabilities.runtimeType == egret.RuntimeType.WXGAME) {
            DataUtils.rawData = await RES.getResByUrl(GlobalManager.instance.resUrl + "data" + GlobalManager.instance.dataVersion + ".json");
            console.log('DataUtils.load = [' + GlobalManager.instance.resUrl + "data" + GlobalManager.instance.dataVersion + ".json" + ']');
        }
        else {
            DataUtils.rawData = await RES.getResAsync("data.json");
            console.log('DataUtils.load = [' + "data.json" + ']');
        }
    }

    public static readConfig(name: string): any {
        return DataUtils.rawData[name];
    }

    /**
     * 根据key查找config配置文件
     */
    public static readConfigWithkey(name: string, key: string): any {
        let _json = DataUtils.rawData[name];
        if (!GameUtils.isEmpty(_json)) {
            if (_json && _json[key]) {
                return _json[key];
            }
        }
        else {
            Logger.debug(name + "文件还没有加载，请检查code");
        }
        return null;
    }

    /**
    * 根据多个特定的键值查找config配置文件
    */
    public static readConfigWithManyKey(name: string, array: Object[]): any {
        if (array.length <= 0) {
            Logger.debug("没有可以检测的键值");
            return null;
        }
        let _json = DataUtils.rawData[name];
        let _list = [];
        if (!GameUtils.isEmpty(_json)) {
            let key = array[0]["key"];
            let value = array[0]["value"];
            _list = DataUtils.readConfigWithKeyAndValue(name, key, value);
            let filters = {};
            for (let i = 0; i < array.length; i++) {
                key = array[i]["key"];
                value = array[i]["value"];
                filters[key] = [value];
            }
            let keys = Object.keys(filters)
            let result = _list.filter(item => {
                return keys.every(t => {
                    return filters[t].toString().indexOf(item[t]) !== -1
                })
            })

            return result;
        }
        else {
            Logger.debug(name + "文件还没有加载，请检查code");
            return null;
        }
    }

    /**
    * 根据特定的键值查找config配置文件
    */
    public static readConfigWithKeyAndValue(name: string, key: string, value: string): any {
        let _json = DataUtils.rawData[name];
        let _list = [];
        if (!GameUtils.isEmpty(_json)) {
            let traverseFind = function (list) {
                for (let i in list) {
                    if (!GameUtils.isEmpty(_json[i]) && _json[i].length > 0) {
                        traverseFind(_json[i]);
                    }
                    else {
                        if (list[i][key] == value) {
                            _list.push(list[i]);
                        }
                    }
                }
            }
            traverseFind(_json);
            return _list;
        }
        else {
            Logger.debug(name + "文件还没有加载，请检查code");
            return null;
        }
    }

    /**
     * 检测功能开启条件(返回关卡ID)
     */
    public static checkOpenConfig(key: string): string {
        let _value = DataUtils.readConfigWithKeyAndValue("Config", "Id", key);
        return _value.length > 0 ? _value[0]["Value"] : null;
    }

}